<template>
  <div>
    <el-dialog v-model="dialogVisible" width="80%" align-center :close-on-click-modal="false" :show-close="false"
      :before-close="handleClose" destroy-on-close draggable>
      <template #header="{ close }">
        <div class="dialog-header flex justify-between items-center">
          <div>期刊{{ ruleForm.id ? '编辑' : '新增' }}</div>
          <el-icon class="el-icon--left cursor-pointer" @click="close">
            <CircleCloseFilled color="#fff" />
          </el-icon>
        </div>
      </template>
      <el-form :model="ruleForm" :rules="rules" label-width="130px" ref="ruleFormRef">
        <!-- 基本信息 -->
        <div class="bg-white mt-[10px]">
          <BaseHead>基本信息</BaseHead>
          <div class="p-[10px]">

            <el-row :gutter="20">

              <el-col :span="24">
                <el-form-item label="期刊名称" prop="name">
                  <el-input placeholder="请输入期刊名称" v-model="ruleForm.name" clearable></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="主办单位">
                  <el-input placeholder="请输入主办单位" v-model="ruleForm.chargeUnit" clearable></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="期刊源名称">
                  {{ route.query.name }}
                </el-form-item>
              </el-col>

              <el-col :span="12">
                <el-form-item label="ISSN号">
                  <el-input placeholder="请输入ISSN号" v-model="ruleForm.issn" clearable></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12"></el-col>
              <el-col :span="12">
                <el-form-item label="影响因子">
                  <el-input placeholder="请输入影响因子" v-model="ruleForm.factor" clearable></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="分区">
                  <el-radio-group v-model="ruleForm.magazinePartition">
                    <el-radio value="1">一区</el-radio>
                    <el-radio value="2">二区</el-radio>
                    <el-radio value="3">三区</el-radio>
                    <el-radio value="4">四区</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="备注">
                  <el-input v-model="ruleForm.note" type="textarea" autosize />
                </el-form-item>
              </el-col>
            </el-row>
          </div>
        </div>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <div class="operate-btns">
            <el-button type="primary" @click="submitForm(ruleFormRef)" :loading="loading">保存</el-button>
            <el-button type="primary" @click="initData">关闭</el-button>
          </div>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import BaseHead from "@/views/project/components/BaseHead.vue";
import { getmagazineAdd, getmagazineUpdate } from "@/api/achievement/kycg"
import { useRoute } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
const dialogVisible = defineModel();
const route = useRoute();
const ruleForm = reactive({
  name: '',
  chargeUnit: '',
  magazineSourceId: route.query.id || "",
  issn: '',
  note: '',
  factor: '',
  magazinePartition: '',
})
const rules = reactive({
  name: [
    { required: true, message: '请输入期刊名称', trigger: 'blur' },
  ],
})
const loading = ref(false)
const ruleFormRef = ref()
const submitForm = async (formEl) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log('submit!')
      loading.value = true;
      let params = {
        ...ruleForm,
      }
      console.log(params, 'params')
      if (params.id) {
        getmagazineUpdate(params).then((res) => {
          if (res.code == 200) {
            ElMessage({
              message: res.msg,
              type: 'success',
            })
            initData()
            emit('callBack', "")
          } else {
            ElMessage({
              message: res.msg,
              type: 'error',
            })
          }
          loading.value = false;
        })
      } else {
        getmagazineAdd(params).then((res) => {
          if (res.code == 200) {
            ElMessage({
              message: res.msg,
              type: 'success',
            })

            initData()
            emit('callBack', "")
          } else {
            ElMessage({
              message: res.msg,
              type: 'error',
            })
          }
          loading.value = false;
        })
      }
    } else {
      console.log('error submit!', fields)
    }
  })
}
const emit = defineEmits(['callBack']);
const handleClose = (done) => {
  Object.assign(ruleForm, {
    name: '',
    chargeUnit: '',
    magazineSourceId: route.query.id || "",
    issn: '',
    note: '',
    factor: '',
    magazinePartition: '',
  })
  delete ruleForm.id;
  emit('callBack', "")
  done()
}
const initData = () => {
  Object.assign(ruleForm, {
    name: '',
    chargeUnit: '',
    magazineSourceId: route.query.id || "",
    issn: '',
    note: '',
    factor: '',
    magazinePartition: '',
  })
  delete ruleForm.id;
  dialogVisible.value = false
}
const getRuleForm = (i) => {
  for (const key in i) {
    ruleForm[key] = i[key];
  }
  console.log(ruleForm, 'ruleForm')
}
defineExpose({
  getRuleForm
})
</script>

<style scoped lang="scss">
@import url("@/views/achievement/index.scss");
</style>
